<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class OxInfo 
{
	public $id;
    public $title;
    public $content;
    private $parent_page; //this is just an id, null or 0 means top level page
    
    private $parent_read;
    private $parent_object;
    
    private $children_read;
    private $children;
    
    function __construct($data = null)
    {
        if(!$data)
        {
            $this->clear();
        }
        else
        {
            $this->id = $data->idinfo_pages;
            $this->title = $data->title;
            $this->content = $data->content;
            $this->parent_page = $data->parent_page; 

            $this->parent_read = false;
            $this->children_read = false;  
        }
	}
    
    function clear()
    {
		$this->id = null;
		$this->title = null;
		$this->content = null;
		$this->parent_page = null;                      
    }
    
    public static function from_id($info_page_id)
    {
        $CI = &get_instance();
        $query = $CI->db->get_where('info_pages', 'idinfo_pages = ' . $info_page_id)->result();
        $new_page = new OxInfo($query[0]);
        return $new_page;
    }
    
    public static function get_children($parent_info_page_id)
    {
        $CI = &get_instance();
        $query = $CI->db->get_where('info_pages', 'parent_page = ' . $parent_info_page_id)->result();
        $children = array();
        foreach($query as $child)
        {
            $children[] = new OxInfo($child);
        }
        return $children;
    }   
    
    public static function get_main_pages()
    {
        $CI = &get_instance();
        $query = $CI->db->get_where('info_pages', 'parent_page is null')->result();
        $main_pages = array();
        foreach($query as $page)
        {
            $main_pages[] = new OxInfo($page);
        }
        return $main_pages;
    }      
    
    public function parent()
    {
        if(!$this->parent_page)
        {
            //there is no parent
            return null;
        }
        if(!$this->parent_read)
        {
            $this->parent_object = self::from_id($this->parent_page);
            $this->parent_read = true;
        }
        return $this->parent_object;
    }
    
    public function children()
    {
        if(!$this->children_read)
        {
            $this->children = self::get_children($this->id);
            $this->children_read = true;
        }
        return $this->children;        
    }
    
    public function showlink($left_padding, $id_current, $show_kids = true)
    {
        $s = '<div class="toc_item" style="padding-left: ' . $left_padding .  'px; ' .
        ($this->id == $id_current?'font-weight: bold;':'') .'">';
        $s .= '<a href="' . site_url('info/' . $this->id) . '">' . $this->title . '</a></div>';
        
        if($show_kids)
        {
            foreach($this->children() as $child)
            {
                $s .= $child->showlink($left_padding + 5, $id_current, true);
            }
        }
        return $s;
    }
    
    public static function TOC($id_current)
    {
        $toc_text = '';
        foreach(self::get_main_pages() as $page)
        {
            $toc_text .= $page->showlink(0, $id_current, true);
        }
        return $toc_text;
    }
}
